<?php


namespace app\api\controller\mjlbb\user;

use app\api\validate\UserValidate;
use app\common\controller\Api;
use app\api\controller\wechat\wechatAppPay;
use app\api\controller\wechat\Wxbizdatacrypt;
use think\Db;
use think\Request;

class User extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    private $appid ="wxf62b39343daf7157";
    private $secret = "a24067f9a1372ce78e99a65163dfa762";
    private $validate;
    
    public function __construct()
    {
        $this->validate = new UserValidate();
    }

    public function GetOpenid()
    {
        $url  = "https://api.weixin.qq.com/sns/jscode2session?";
        $url .= "appid=" . $this->appid . "&";
        $url .= "secret=". $this->secret."&";
        $url .= "js_code=" . input('code') . "&";
        $url .= "grant_type=authorization_code";
        $html = file_get_contents($url);
        return $html;
    }

    /**
     * @记录用户登陆
     */
    public function insertUser($openid)
    {
        $param = [
            'openid' => $openid,
            'user_is_vip' => 2,
        ];
        
        if(!$this->validate->scene('openid')->check($param)){
            ajax_info(1, $this->validate->getError(), []);
        }
        
        $user = Db::table('fa_shopro_user')->where($param)->find();
        if(!$user){
            $info = Db::table('fa_shopro_user')->insertGetId($param);
            ajax_info(0, '记录vip用户成功', $info); //返回用户插入id
        }
        ajax_info(0, 'success', ['已经记录了']);
    }


    /**
     * @获取用户信息接口
     */
    public function GetuserInfo(Request $request): bool
    {
        $req = [
            'user_id' => $request->param('user_id'),
            'openid' => $request->param('openid'),
        ];
        if(!$this->validate->scene('openid')->check($req)){
            ajax_info(1, $this->validate->getError());
        }
        $where = [];
        if($req['user_id']){
            $where['user_id'] = ['eq', $req['user_id']];
        }
        if($req['openid']){
            $where['openid'] = ['eq', $req['openid']];
        }
        $info = Db::table('fa_shopro_user')
            ->where($where)
            ->find();
        if(!$info)
        {
            ajax_info(1, '无用户信息');
        }
        ajax_info(0, '有', $info);

    }

    /**
     * @创建会员订单（注册）
     */
    public function createVipOrder(Request $request)
    {
        $req = [
            'openid'  => $request->param('openid'),
            'goods_id' => $request->param('goods_id'),
            'goods_num' => $request->param('goods_num'),
            'order_pay_money' => $request->param('order_pay_money'),
            'create_add_vip_time' => time(),
            'order_typ' => $request->param('order_typ'),
            'order_get_month' => 180,
        ];

        if(!$this->validate->scene('PayRegisterVip')->check($req)){
            ajax_info(1, $this->validate->getError());
        }

        $info = Db::table('fa_shopro_viporder')->insertGetId($req);
        if(!$info)
        {
            ajax_info(1, '更新失败');
        }
        ajax_info(0, '更新成功', $info);
    }

    /**
     * NAME:支付订单接口
     */
    public function payVipOrder(Request $request)
    {
        $req = [
            'orderid' => $request->param('orderid'),
            'openid' => $request->param('openid'),
        ];
        if(!$this->validate->scene('orderid')->check($req)){
            ajax_info(1, $this->validate->getError());
        }

        $this->NotifySuccess($req['orderid'], $req['openid']);
    }

    /**
     * @会员支付成功回调接口
     */
    public function NotifySuccess($order_sn = 0, $openid = 0)
    {
//        $post_str = file_get_contents('php://input');
//        $post_data = (array)simplexml_load_string($post_str, 'SimpleXMLElement', LIBXML_NOCDATA);

//        $post_data['out_trade_no'] = 'mjlbb' . 1;
//        $post_data['total_fee'] = 1222;
//
//        $order_sn = $post_data['out_trade_no'];
//        $total_fee = $post_data['total_fee'] * 0.01;
//
//        $order_sn = trim(substr($order_sn, 5, 11));
//
//        var_dump($order_sn);

        /**
         * 更新支付订单
         */

        Db::table('fa_shopro_viporder')
            ->where('id', $order_sn)
            ->update([
                'pay_success_time' => time(),
                'pay_wechat_money'=> 1.00
            ]);

        $this->insertUser($openid);
        /**
         * 更新用户表 成为会员
         */


    }
}
